package com.dhsc.service.impl.wskh;

import com.dhsc.domain.ywzx.wskh.WskhHdywVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.domain.yypt.sys.TXtdmVo;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.common.ICommonDao;
import com.dhsc.local.dao.wskh.IWskhHdywDao;
import com.dhsc.service.wskh.IWskhHdywService;
import com.dhsc.util.CommonUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class WskhHdywServiceImpl implements IWskhHdywService{

	@Autowired
	private IWskhHdywDao twskhhdywDao;

	@Autowired
	private ICommonDao commonDao;

	@Override
	public LayuiTableVo queryWskhHdywByAll(WskhHdywVo y) {
		LayuiTableVo obj = new LayuiTableVo();
		try{
			PageMethod.startPage(y.getPage(),y.getLimit());
			List<WskhHdywVo> list = twskhhdywDao.queryWskhHdywByAll(y);
			PageInfo<WskhHdywVo> pageInfo = new PageInfo<>(list);

			TXtdmVo x = new TXtdmVo();
			x.setFldmstr("WSKHHDYW_YWLB;KHHF_CLBZ;KHHF_FSBZ;KHHF_REBZ");
			List<TXtdmVo> tXtdmVos = commonDao.queryQsTXtdmByFldmStr(x);
			for (int i = 0; i < list.size(); i++) {
				WskhHdywVo wskhHdywVo = list.get(i);
				for (int j = 0; j < tXtdmVos.size(); j++) {
					if ("WSKHHDYW_YWLB".equals(tXtdmVos.get(j).getFldm())){
						if (CommonUtil.isNotEmptyObj(wskhHdywVo.getYwlb()) && wskhHdywVo.getYwlb().equals(tXtdmVos.get(j).getValue())){
							wskhHdywVo.setYwlb_dis(tXtdmVos.get(j).getName());
						}
					}
					if ("KHHF_CLBZ".equals(tXtdmVos.get(j).getFldm())){
						if (CommonUtil.isNotEmptyObj(wskhHdywVo.getClbz()) && wskhHdywVo.getClbz().equals(tXtdmVos.get(j).getValue())){
							wskhHdywVo.setClbz(tXtdmVos.get(j).getName());
						}
					}
					if ("KHHF_FSBZ".equals(tXtdmVos.get(j).getFldm())){
						if (CommonUtil.isNotEmptyObj(wskhHdywVo.getFsbz()) && wskhHdywVo.getFsbz().equals(tXtdmVos.get(j).getValue())){
							wskhHdywVo.setFsbz(tXtdmVos.get(j).getName());
						}
					}
					if ("KHHF_REBZ".equals(tXtdmVos.get(j).getFldm())){
						if (CommonUtil.isNotEmptyObj(wskhHdywVo.getRebz()) && wskhHdywVo.getRebz().equals(tXtdmVos.get(j).getValue())){
							wskhHdywVo.setRebz(tXtdmVos.get(j).getName());
						}
					}
				}
			}

			obj.setData(list);
			obj.setMsg("");
			obj.setCount(pageInfo.getTotal());
			obj.setCode(0);
		}catch(Exception e){
			log.error("查询互动客户业务处理出错", e);
			throw new MyException("查询互动客户业务处理出错",e);
		}
		return obj;
	}
	
	@Override
	public ResponseObject saveWskhHdyw(WskhHdywVo y) {
		ResponseObject obj = new ResponseObject();
		try{
			if(y.getType() == 1)
			{
				twskhhdywDao.insertWskhHdyw(y);
			}else{
				twskhhdywDao.updateWskhHdyw(y);
			}
			obj.setStatus(200);
		}catch(Exception e){
			log.error("保存互动客户业务-待处理回访信息出错", e);
			throw new MyException("保存互动客户业务-待处理回访信息出错",e);
		}
		return obj;
	}

	@Override
	public ResponseObject updateWskhDxjhmx(WskhHdywVo y) {
		ResponseObject obj = new ResponseObject();
		try{
			twskhhdywDao.updateWskhClsm(y);
			obj.setStatus(200);
		}catch(Exception e){
			log.error("修改短信交互明细信息出错", e);
			throw new MyException("修改短信交互明细信息出错",e);
		}
		return obj;
	}
}